
package com.yushu.common.util.result;

import java.util.HashMap;


 /**
 * 前后端返回结果集
 * @author test
 * @CreateDate 2020-12-03 15:47:24
 */
public class Result<T> extends HashMap<String, Object> {
    private String code;
    private String msg;
    private T data;

    public Result() {
    }

    public String getCode() {
        return this.code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return this.msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }


    public static Result success(Object object) {
        Result result = new Result();
        result.setData(object);
        return result;
    }

    public static Result success(String msg, Object object) {
        Result result = new Result();
        result.put("info", msg);
        result.put("type", "success");
        result.put("data",object);
//        result.setData(JSONObject.toJSON(object));
        return result;
    }

    public static Result success(String msg) {
        Result result = new Result();
        result.put("info", msg);
        result.put("type", "success");
        result.setMsg(msg);
        return result;
    }

    public static Result success() {
        return success((Object) null);
    }

    public static Result error(String code, String format, String... arg) {
        Result result = new Result();
        result.setCode(code);
        String resultMsg = String.format(format, arg);
        result.setMsg(resultMsg);
        return result;
    }

    public static Result error(String msg) {
        Result result = new Result();
        result.put("info", msg);
        result.put("type", "error");
        result.setMsg(msg);
        return result;
    }
}
